<template>
  <div class="wrapper">
    <!-- header部分 -->
    <header>
      <p>用户注册</p>
    </header>
    <!-- 表单部分 -->
    <ul class="form-box">
      <li class="sty1">
        <div class="title">
          手机号码：
        </div>
        <div class="content">
          <input type="text" @blur="checkUserId" v-model="user.userId" placeholder="手
机号码" oninput="value=value.replace(/[^\d]/g,'')" id="mobileNo">
        </div>
      </li>
      <li class="sty1">
        <div class="title">
          密码：
        </div>
        <div class="content">
          <input type="password" v-model="user.password" placeholder="密码 需包括字母，数字，特殊字符">
        </div>
      </li>
      <li class="sty1">
        <div class="title">
          确认密码：
        </div>
        <div class="content">
          <input type="password" v-model="confirmPassword" placeholder="确认密码">
        </div>
      </li>
      <li class="sty1">
        <div class="title">
          用户名称：
        </div>
        <div class="content">
          <input type="text" v-model="user.userName" placeholder="用户名称">
        </div>
      </li>
      <li class="sty1">
        <div class="title">
          性别：
        </div>
        <div class="content" style="font-size: 3vw;">
          <input type="radio" v-model="user.userSex" value="1" style="width:6vw;height: 3.2vw;">男
          <input type="radio" v-model="user.userSex" value="0" style="width:6vw;height: 3.2vw;">女
        </div>
      </li>
      <li class="sty2">
        <div class="title">
          用户头像：
        </div>
        <ul class="imgtype">
          <li @click="selectImg(1)">
            <img src="../assets/t1.png">
          </li>
          <li @click="selectImg(2)">
            <img src="../assets/t2.png">
          </li>
          <li @click="selectImg(3)">
            <img src="../assets/t3.png">
          </li>
          <li @click="selectImg(4)">
            <img src="../assets/t4.png">
          </li>
          <li @click="selectImg(5)">
            <img src="../assets/t5.png">
          </li>
          <li @click="selectImg(6)">
            <img src="../assets/t6.png">
          </li>
          <li @click="selectImg(7)">
            <img src="../assets/t7.png">
          </li>
          <li @click="selectImg(8)">
            <img src="../assets/t8.png">
          </li>
          <li @click="selectImg(9)">
            <img src="../assets/t9.png">
          </li>
        </ul>
      </li>
    </ul>

    <div class="button-login">
      <button @click="register">注册</button>
    </div>

    <!-- 底部菜单部分 -->
    <Footer></Footer>
  </div>
</template>
<script>
import Footer from '../components/Footer.vue';
import CryptoJS from 'crypto-js';

export default {
  name: 'Register',
  data() {
    return {
      user: {
        userId: '',
        password: '',
        userName: '',
        userImg:'',
        userSex: 1,
        delTag:1

        /*需新加余额id与积分id,与207行对应     16:16 0904
        walletId:'',
        Id:''
         */
      },
      virtualwallet:{},
      integral:{},
      confirmPassword: ''
    }
  },
  methods: {
    selectImg(num){
      this.$axios.post('ImageController/listImageById', this.$qs.stringify({
        imgId: num
      })).then(response => {
        let result = response.data;
        this.user.userImg = result;
      }).catch(error => {
        console.error(error);
      });
    },
    checkUserId() {
      this.$axios.post('UserController/getUserById', this.$qs.stringify({
        userId: this.user.userId,
      })).then(response => {
        if (response.data == 1) {
          this.user.userId = '';
          alert('此手机号码已存在！')
        }
      }).catch(error => {
        console.error(error);
      });
    },
    register() {
      var reg = /^1[34578]\d{9}$/;
      if (!reg.test(this.user.userId)) {
        alert('手机号码格式错误，请检查！');
        return;
      }

      var passwordreg = /(?=.*\d)(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{6,16}/;
      if (!passwordreg.test(this.user.password)) {
        alert('密码格式不正确，需包括字母，数字，特殊字符！');
        return;
      }
      if (this.user.password != this.confirmPassword) {
        alert('两次输入的密码不一致！');
        return;
      }
      if (this.user.userName == '') {
        alert('用户名不能为空！');
        return;
      }
      if (this.user.userImg == '') {
        alert('请选择用户头像！');
        return;
      }


      this.user.password=CryptoJS.SHA256(this.user.password).toString()

      //申请钱包
      this.$axios.post('WalletController/saveWallet', this.$qs.stringify({
        userId: this.user.userId
      })).then(response => {
        if (response.data > 0) {
        //  alert('钱包申请成功！');
          this.$router.go(-1);
        } else {
          alert('钱包申请失败！');
        }
      }).catch(error =>{
        console.error(error);
      })


      //申请积分
      this.$axios.post('IntegrationlogController/saveIntegrationlogByPay', this.$qs.stringify({
        userId: this.user.userId,
        count:10,
        type:1
      })).then(response => {
        if (response.data > 0) {

          //增加单个积分表明细
          this.$axios.post('IntegrationlogController/saveIntegrationlogByPay', this.$qs.stringify({
            userId: this.user.userId,
            count:10,
            type:1
          })).then(response => {

          }).catch(error =>{
            console.error(error);
          });
          //待更改
          //更新积分总表明细
          this.$axios.post('IntegralTransController/saveIntegralTrans', this.$qs.stringify({
            userId: this.user.userId,
            count:10,
            type:1
          })).then(response => {

          }).catch(error =>{
            console.error(error);
          });

        //  alert('积分申请成功！');
          //this.$router.go(-1);
        } else {
          alert('积分申请失败！');
        }
      }).catch(error =>{
        console.error(error);
      })



      //注册请求
      this.$axios.post('UserController/saveUser', this.$qs.stringify(
          this.user
      )).then(response => {
        if (response.data > 0) {
          alert('注册成功！');
          //this.$router.go(-1);
        } else {
          alert('注册失败！');
        }
      }).catch(error => {
        console.error(error);
      });
    }
  },
  components: {
    Footer
  }
}
</script>
<style scoped>
/****************** 总容器 ******************/
.wrapper {
  width: 100%;
  height: 100%;
}

/****************** header部分 ******************/
.wrapper header {
  width: 100%;
  height: 12vw;
  background: linear-gradient(#F37373, #F08080, #EF9292);
  color: #fff;
  font-size: 4.8vw;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 1000;
  display: flex;
  justify-content: center;
  align-items: center;
}

/****************** 表单部分 ******************/
.wrapper .form-box {
  width: 100%;
  margin-top: 12vw;
}

.wrapper .form-box .sty1 {
  box-sizing: border-box;
  padding: 4vw 3vw 0 3vw;
  display: flex;
  align-items: center;
}

.wrapper .form-box .sty2 {
  box-sizing: border-box;
  padding: 4vw 3vw 0 3vw;
  display: flex;
  flex-direction: column;
}

.wrapper .form-box .sty1 .title {
  flex: 0 0 18vw;
  font-size: 3vw;
  font-weight: 700;
  color: #666;
}
.wrapper .form-box .sty2 .title {
  flex: 0 0 10vw;
  font-size: 3vw;
  font-weight: 700;
  color: #666;
}
.wrapper .form-box .sty2 .imgtype {
  width: 90%;
  display: flex;
  /*自动换行*/
  flex-wrap: wrap;
  justify-content: space-around;
  /*把两行当成整体居中 align-items各自居中*/
  align-content: center;
  margin-left: 18px;
}

.wrapper .form-box .sty2 .imgtype li{
  /*实现一排3个*/
  width: 33.33%;
  height: 33.33%;

  display: flex;
  /*改变主轴方向*/
  flex-direction: column;
  justify-content: center;
  align-items: center;

  user-select: none;
  /*鼠标悬浮变成手的样式*/
  cursor: pointer;
}

.wrapper .form-box .sty2 .imgtype li img {
  width: 70%;
  height: auto;
  border-radius: 50%;
  padding-bottom: 20px;
}

.wrapper .form-box .sty1 .content {
  flex: 1;
}

.wrapper .form-box .sty1 .content input {
  border: none;
  outline: none;
  width: 100%;
  height: 4vw;
  font-size: 3vw;
}

.wrapper .button-login {
  width: 100%;
  box-sizing: border-box;
  padding: 4vw 3vw 0 3vw;
}

.wrapper .button-login button {
  width: 100%;
  height: 10vw;
  font-size: 3.8vw;
  font-weight: 700;
  color: #fff;
  background: linear-gradient(#F37373, #F08080, #EF9292);
  border-radius: 4px;
  border: none;
  outline: none;
}

.wrapper .button-register {
  width: 100%;
  box-sizing: border-box;
  padding: 4vw 3vw 0 3vw;
}

.wrapper .button-register button {
  width: 100%;
  height: 10vw;
  font-size: 3.8vw;
  font-weight: 700;
  color: #666;
  background-color: #EEE;
  border-radius: 4px;
  border: none;
  outline: none;
  border: solid 1px #DDD;
}
</style>
